package javaTest05.utils;

import com.alibaba.fastjson.JSON;
import javaTest05.exercise02.dto.MessageDto;

import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;

public class SocketUtils {

    public final static int BUFFER_SIZE = 1024;

    public final static String BLANK = "\0";

    public final static String BLANK_STR = "";

    public final static String QUIT_FLAG = "wq!";

    public final static String TO_FLAG = " /st ";

    public static void close(Closeable... closeable) {//安全关闭IO流
        for (Closeable able : closeable) {
            try {
                if (able instanceof Flushable) {
                    ((Flushable) able).flush();
                }
                able.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static String messageToJson(MessageDto messageDto) {
        return JSON.toJSONString(messageDto);
    }

    public static MessageDto jsonToMessage(String json) {
        return JSON.parseObject(json, MessageDto.class);
    }
}
